package com.xiao.sulingproduct.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiao.sulingproduct.entity.BusinessEntity;
import com.xiao.sulingproduct.fegin.EsFeign;
import com.xiao.sulingproduct.service.BusinessService;
import com.xiao.sulingproduct.vo.BusinessVo;
import com.xiao.sulingpublic.to.BusinessTo;
import com.xiao.sulingpublic.to.ShopEsTo;
import com.xiao.sulingpublic.utils.PageUtils;
import com.xiao.sulingpublic.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;




/**
 * 创建商家表
 *
 * @author wen
 * @email 1322127758@qq.com
 * @date 2022-03-11 14:46:16
 */
@RestController
@RequestMapping("sulingpxqg/business")
public class BusinessController {
    @Autowired
    private EsFeign esFeign;

    @Autowired
    private BusinessService businessService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("sulingpxqg:business:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = businessService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{buId}")
    //@RequiresPermissions("sulingpxqg:business:info")
    public R info(@PathVariable("buId") Long buId){
		BusinessEntity business = businessService.getById(buId);

        return R.ok().put("business", business);
    }

    /**
     * 通过用户号也是店铺号获取店铺信息
     * @param buMemberUsername 用户号也是店铺号
     */
    @RequestMapping("/getbusiness")
    //@RequiresPermissions("sulingpxqg:business:info")
    public R getBusiness(@RequestParam("buMemberUsername") String buMemberUsername){
        BusinessEntity business = businessService.getOne(new QueryWrapper<BusinessEntity>().eq("bu_member_username",buMemberUsername));
        BusinessTo businessTo = new BusinessTo();
        BeanUtils.copyProperties(business,businessTo);
        return R.ok().setData("businessTo", businessTo);
    }

    /**
     * 获取店铺信息
     * @param memberUsername :用户号
     * @return 返回店铺信息
     */
    @GetMapping("/select/business")
    public R selectByMemberUsername(@RequestParam("memberUsername") String memberUsername){
        BusinessVo businessData = null;
        if(!StringUtils.isEmpty(memberUsername)){
            businessData = businessService.selectByMemberUsername(memberUsername);
        }

        return R.ok().put("businessData",businessData);
    }

    /**
     * 获取附近店铺集合
     * @param    shopEsTo 经度 纬度 距离（千米）
     **/
    @RequestMapping("/select/shops")
    public R selectByLatAndLon( ShopEsTo shopEsTo) throws IOException {
        return esFeign.selectByLatAndLon(shopEsTo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("sulingpxqg:business:save")
    public R save(@RequestBody BusinessEntity business){
		businessService.save(business);

        return R.ok();
    }

    /**
     * 保存或修改店铺数据
     * @param businessVo 要保存或修改的数据
     * @return 返回状态码
     */
    @PostMapping("/saveorup")
    public Integer saveOrUp( BusinessVo businessVo) throws IOException {
        if(!ObjectUtils.isEmpty(businessVo) && null != businessVo.getBuMemberUsername()){
            return businessService.saveOrUp(businessVo);
        }
        return null;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("sulingpxqg:business:update")
    public R update(@RequestBody BusinessEntity business){
		businessService.updateById(business);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("sulingpxqg:business:delete")
    public R delete(@RequestBody Long[] buIds){
		businessService.removeByIds(Arrays.asList(buIds));

        return R.ok();
    }

}
